using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Utilities;



namespace Services.Menu
{
    public enum HourSelectorType
    {
        HoursMinutesSeconds,
        HoursMinutes,
        Hours,
        Minutes,
        MinutesSeconds,
        Seconds
    }
    
    public class TimeSelectorScreen : ASelectorScreen
    {
        HourSelectorType type;


        public TimeSelectorScreen(Game game, Action<DateTime> closeSelector, Action abortSelection,
            TimeSpan defaultTimeSpan, HourSelectorType type, String titleString)
            : base(game, closeSelector, abortSelection, defaultTimeSpan.Hours, defaultTimeSpan.Minutes, defaultTimeSpan.Seconds, (SelectorType)type, titleString)
        {
            this.type = type;

        }

        public override void Initialize()
        {
            
            base.Initialize();
            
        }

        public override void SetPortraitOrientation()
        {
            /*
            confirm = new MyButton(new Rectangle(160, 720, 60, 60), ok, Color.White, circle, Color.White);
            cancel = new MyButton(new Rectangle(260, 720, 60, 60), no, Color.White, circle, Color.White);

            upper_box = new MyButton(new Rectangle(0, 0, 480, 80), white, Color.Black);
            lower_box = new MyButton(new Rectangle(0, 700, 480, 100), white, Color.Black);
             * */
            listA.Clear();
            listB.Clear();
            listC.Clear();
            int block_size = 140;
            switch (type)
            {
                case HourSelectorType.Hours:
                case HourSelectorType.Minutes:
                case HourSelectorType.Seconds:
                    if (type == HourSelectorType.Hours)
                    {
                        for (int i = 0; i < 24; i++)
                        {
                            listA.Add(new MyButton(new Rectangle(240 - block_size / 2, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                               "" + i, Color.White, font, TextPos.Center, box, Color.LightGray));
                        }
                        areaA = new Rectangle(240 - block_size / 2, 400 - block_size / 2, block_size, block_size);
                        dragAreaA = new Rectangle(240 - block_size / 2, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    else if (type == HourSelectorType.Minutes)
                    {
                        for (int i = 0; i < 60; i++)
                            listB.Add(new MyButton(new Rectangle(240 - block_size / 2, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaB = new Rectangle(240 - block_size / 2, 400 - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(240 - block_size / 2, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    else if (type == HourSelectorType.Seconds)
                    {
                        for (int i = 0; i < 60; i++)
                            listC.Add(new MyButton(new Rectangle(240 - block_size / 2, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaC = new Rectangle(240 - block_size / 2, 400 - block_size / 2, block_size, block_size);
                        dragAreaC = new Rectangle(240 - block_size / 2, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }

                    break;
                case HourSelectorType.HoursMinutes:
                case HourSelectorType.MinutesSeconds:
                    if (type == HourSelectorType.HoursMinutes)
                    {
                        for (int i = 0; i < 24; i++)
                            listA.Add(new MyButton(new Rectangle(235 - block_size, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                "" + i, Color.White, font, TextPos.Center, box, Color.LightGray));
                        for (int i = 0; i < 60; i++)
                            listB.Add(new MyButton(new Rectangle(245, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaA = new Rectangle(235 - block_size, 400 - block_size / 2, block_size, block_size);
                        areaB = new Rectangle(245, 400 - block_size / 2, block_size, block_size);

                        dragAreaA = new Rectangle(235 - block_size, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                        dragAreaB = new Rectangle(245, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    else
                    {

                        for (int i = 0; i < 60; i++)
                            listB.Add(new MyButton(new Rectangle(235 - block_size, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaB = new Rectangle(235 - block_size, 400 - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(235 - block_size, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);

                        for (int i = 0; i < 60; i++)
                            listC.Add(new MyButton(new Rectangle(245, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaC = new Rectangle(245, 400 - block_size / 2, block_size, block_size);
                        dragAreaC = new Rectangle(245, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    break;
                case HourSelectorType.HoursMinutesSeconds:
                    {

                        for (int i = 0; i < 24; i++)
                        {
                            listA.Add(new MyButton(new Rectangle(240 - block_size / 2 - 10 - block_size, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                               "" + i, Color.White, font, TextPos.Center, box, Color.LightGray));
                        }
                        areaA = new Rectangle(240 - block_size / 2 - 10 - block_size, 400 - block_size / 2, block_size, block_size);
                        dragAreaA = new Rectangle(240 - block_size / 2 - 10 - block_size, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);


                        for (int i = 0; i < 60; i++)
                            listB.Add(new MyButton(new Rectangle(240 - block_size / 2, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaB = new Rectangle(240 - block_size, 400 - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(240 - block_size / 2, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);

                        for (int i = 0; i < 60; i++)
                            listC.Add(new MyButton(new Rectangle(240 + block_size / 2 + 10, 400 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaC = new Rectangle(240 + block_size / 2 + 10, 400 - block_size / 2, block_size, block_size);
                        dragAreaC = new Rectangle(240 + block_size / 2 + 10, upper_box.rectangle.Height,
                            block_size, 800 - upper_box.rectangle.Height - upper_box.rectangle.Height);

                        break;
                    }
            }
            UpdateSelectedValue();
        }

        public override void SetLandScapeOrientation()
        {
            /*if (prev_orientation == DisplayOrientation.LandscapeRight)
            {
                confirm = new MyButton(new Rectangle(20, 160, 60, 60), ok, Color.White, circle, Color.White);
                cancel = new MyButton(new Rectangle(20, 260, 60, 60), no, Color.White, circle, Color.White);
            }
            else
            {
                confirm = new MyButton(new Rectangle(720, 160, 60, 60), ok, Color.White, circle, Color.White);
                cancel = new MyButton(new Rectangle(720, 260, 60, 60), no, Color.White, circle, Color.White);
            }
            

            upper_box = new MyButton(new Rectangle(0, 0, 800, 80), white, Color.Black);
            //lower_box = new MyButton(new Rectangle(0, 700, 480, 100), white, Color.Black);
            lower_box = new MyButton(new Rectangle(0, 480, 0, 0), white, Color.Black);
             * */
            listA.Clear();
            listB.Clear();
            listC.Clear();
            int block_size = 140;
            switch (type)
            {
                case HourSelectorType.Hours:
                case HourSelectorType.Minutes:
                case HourSelectorType.Seconds:
                    if (type == HourSelectorType.Hours)
                    {
                        for (int i = 0; i < 24; i++)
                        {
                            listA.Add(new MyButton(new Rectangle(400 - block_size / 2, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                               "" + i, Color.White, font, TextPos.Center, box, Color.LightGray));
                        }
                        areaA = new Rectangle(400 - block_size / 2, 240 - block_size / 2, block_size, block_size);
                        dragAreaA = new Rectangle(400 - block_size / 2, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    else if (type == HourSelectorType.Minutes)
                    {
                        for (int i = 0; i < 60; i++)
                            listB.Add(new MyButton(new Rectangle(400 - block_size / 2, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaB = new Rectangle(400 - block_size / 2, 240 - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(400 - block_size / 2, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    else if (type == HourSelectorType.Seconds)
                    {
                        for (int i = 0; i < 60; i++)
                            listC.Add(new MyButton(new Rectangle(400 - block_size / 2, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaC = new Rectangle(400 - block_size / 2, 240 - block_size / 2, block_size, block_size);
                        dragAreaC = new Rectangle(400 - block_size / 2, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }

                    break;
                case HourSelectorType.HoursMinutes:
                case HourSelectorType.MinutesSeconds:
                    if (type == HourSelectorType.HoursMinutes)
                    {
                        for (int i = 0; i < 24; i++)
                            listA.Add(new MyButton(new Rectangle(395 - block_size, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                "" + i, Color.White, font, TextPos.Center, box, Color.LightGray));
                        for (int i = 0; i < 60; i++)
                            listB.Add(new MyButton(new Rectangle(405, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaA = new Rectangle(395 - block_size, 240 - block_size / 2, block_size, block_size);
                        areaB = new Rectangle(405, 240 - block_size / 2, block_size, block_size);

                        dragAreaA = new Rectangle(395 - block_size, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                        dragAreaB = new Rectangle(405, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    else
                    {

                        for (int i = 0; i < 60; i++)
                            listB.Add(new MyButton(new Rectangle(395 - block_size, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaB = new Rectangle(395 - block_size, 240 - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(395 - block_size, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);

                        for (int i = 0; i < 60; i++)
                            listC.Add(new MyButton(new Rectangle(405, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaC = new Rectangle(405, 240 - block_size / 2, block_size, block_size);
                        dragAreaC = new Rectangle(405, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);
                    }
                    break;
                case HourSelectorType.HoursMinutesSeconds:
                    {

                        for (int i = 0; i < 24; i++)
                        {
                            listA.Add(new MyButton(new Rectangle(400 - block_size / 2 - 10 - block_size, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                               "" + i, Color.White, font, TextPos.Center, box, Color.LightGray));
                        }
                        areaA = new Rectangle(400 - block_size / 2 - 10 - block_size, 240 - block_size / 2, block_size, block_size);
                        dragAreaA = new Rectangle(400 - block_size / 2 - 10 - block_size, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);


                        for (int i = 0; i < 60; i++)
                            listB.Add(new MyButton(new Rectangle(400 - block_size / 2, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaB = new Rectangle(400 - block_size, 240 - block_size / 2, block_size, block_size);
                        dragAreaB = new Rectangle(400 - block_size / 2, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);

                        for (int i = 0; i < 60; i++)
                            listC.Add(new MyButton(new Rectangle(400 + block_size / 2 + 10, 240 - block_size / 2 + (block_size + 10) * i, block_size, block_size),
                                (i <= 9 ? "0" + i : "" + i), Color.White, font, TextPos.Center, box, Color.LightGray));
                        areaC = new Rectangle(400 + block_size / 2 + 10, 240 - block_size / 2, block_size, block_size);
                        dragAreaC = new Rectangle(400 + block_size / 2 + 10, upper_box.rectangle.Height,
                            block_size, 480 - upper_box.rectangle.Height - upper_box.rectangle.Height);

                        break;
                    }
            }
            UpdateSelectedValue();
        }

        public override void ExitSelector()
        {

            DateTime timeSpan;
            int year = DateTime.Now.Year;
            int month = DateTime.Now.Month;
            int day = DateTime.Now.Day;
            switch (type)
            {
                case HourSelectorType.Hours:
                    timeSpan = new DateTime(year, month, day, selectedAValue, 0, 0);
                    break;
                case HourSelectorType.HoursMinutes:
                    timeSpan = new DateTime(year, month, day, selectedAValue, selectedBValue, 0);
                    break;
                case HourSelectorType.HoursMinutesSeconds:
                    timeSpan = new DateTime(year, month, day, selectedAValue, selectedBValue, selectedCValue);
                    break;
                case HourSelectorType.Minutes:
                    timeSpan = new DateTime(year, month, day, 0, selectedBValue, 0);
                    break;
                case HourSelectorType.MinutesSeconds:
                    timeSpan = new DateTime(year, month, day, 0, selectedBValue, selectedCValue);
                    break;
                case HourSelectorType.Seconds:
                    timeSpan = new DateTime(year, month, day, 0, 0, selectedCValue);
                    break;
                default:
                    timeSpan = new DateTime(year, month, day, 0, 0, 0);
                        
                    break;
            }

            closeSelector(timeSpan);
        }
    }
}
